package org.codeCard.panel;

import cn.hutool.core.bean.BeanUtil;
import com.tool.btn.FormsOnOffBtn;
import com.tool.role.RoleData;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;
import come.tool.JDialog.TiShiUtil;
import org.apache.commons.lang.StringUtils;
import org.cbg.bean.SearchGoodsBean;
import org.cbg.btn.TrslationBtn;
import org.codeCard.bean.TbSaleCardOrder;
import org.codeCard.bean.TbSaleCardOrderExt;
import org.codeCard.btn.CodeCardBtn;
import org.codeCard.btn.DianKaBuyBtn;
import org.codeCard.frame.CodeCardJFrame;
import org.codeCard.model.CodeCardSellerModel;
import org.codeCard.util.CodeCardUntil;
import org.come.Frame.OptionsJframe;
import org.come.Frame.ZhuFrame;
import org.come.bean.LoginResult;
import org.come.entity.Goodstable;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.CutButtonImage;
import org.come.until.FormsManagement;
import org.come.until.GsonUtil;
import org.come.until.Util;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.math.BigDecimal;

public class CodeCardBuyJPanel extends JPanel {

    public static boolean FLAG = true;
    private static JTextField findName,findName1;

    private Goodstable good;
    private static String[] Fvalue;
    private static JTextArea testMes;// 用来显示聊天信息
    private CodeCardBtn bjczBtn1, bjczBtn2;

    private int dangqianyeshu = 1, zuidayema = 50;

    private CodeCardSellerModel codeCardSellerModel;

    BigDecimal middlePay = new BigDecimal("0.05");

    public TbSaleCardOrder customerOrder;

    public TbSaleCardOrderExt customerOrderExt;

    private JScrollPane jScrollPane;

    private JLabel chooseLeft, chooseRight, yema, upSort, downSort;

    public String dianka;
    public String zongjia;
    public String meidian;

    //购买点数
    public JTextField buyCodeCard;

    private DianKaBuyBtn buy;

    /**
     * 页码按钮，首页，末页 , 左箭头, 右箭头
     */
    private TrslationBtn  leftArrows, rightArrows;

    private JLabel zhanshidikuangLab, tupianLab, dahuabishangLab, diankaLab, zongjiaLab, danjiaLab;

    public CodeCardBuyJPanel() {

        jScrollPane = new JScrollPane();
        CodeCardUntil.TableModel(jScrollPane, 3);
        jScrollPane.getVerticalScrollBar().setPreferredSize(new Dimension(0, 0));
        jScrollPane.getViewport().setBorder(null);
        jScrollPane.setViewportBorder(null);
        jScrollPane.setBorder(null);
        jScrollPane.getVerticalScrollBar().setUnitIncrement(10);

        yema = new JLabel(dangqianyeshu + "/" + zuidayema);
        yema.setForeground(Color.white);
        yema.setOpaque(false);
        yema.setHorizontalAlignment(SwingConstants.CENTER);

        this.buyCodeCard = new JTextField();
        setBuyCodeCard();

        if (MyIsif.getStyle().equals("水墨UI")) {

            buy = new DianKaBuyBtn("inkImg/number/b9/水墨购买.png", 1, UIUtils.COLOR_BTNPUTONG, UIUtils.TEXT_HY88, "", this);
            buy.setBounds(207, 448, 99, 30);

            leftArrows = new TrslationBtn("inkImg/number/b9/水墨上一页.png", 1);
            leftArrows.setBounds(176, 307, 50, 20);

            rightArrows = new TrslationBtn("inkImg/number/b9/水墨下一页.png", 1);
            rightArrows.setBounds(286, 307, 50, 20);

            yema.setBounds(230, 310, 58, 17);
            jScrollPane.setBounds(60, 100, 390, 180);

            this.setPreferredSize(new Dimension(500,520));
            this.setLayout(null);
            this.setBackground(UIUtils.Color_BACK);

            // 关闭按钮事件
            FormsOnOffBtn offBtn = new FormsOnOffBtn("skin/8100226/button/1.png", 1, 690);
            offBtn.setBounds(442, 10, 25, 25);
            this.add(offBtn);

        }else {

            buy = new DianKaBuyBtn("inkImg/number/b9/红木购买.png", 1, UIUtils.COLOR_BTNPUTONG, UIUtils.TEXT_HY88, "", this);
            buy.setBounds(178, 458, 99, 30);

            leftArrows = new TrslationBtn("inkImg/number/b9/红木上一页.png", 1);
            leftArrows.setBounds(150, 322, 50, 20);

            rightArrows = new TrslationBtn("inkImg/number/b9/红木下一页.png", 1);
            rightArrows.setBounds(256, 322, 50, 20);

            yema.setBounds(195, 325, 58, 17);
            jScrollPane.setBounds(40, 120, 390, 180);

            this.setPreferredSize(new Dimension(452,527));
            this.setLayout(null);
            this.setBackground(UIUtils.Color_BACK);

            // 关闭按钮事件
            FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/hongmu/1/s74.png", 1, 690);
            offBtn.setBounds(432, 0, 25, 25);
            this.add(offBtn);
        }


        buy.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub

                if (!Util.canBuyOrno) {
                    ZhuFrame.getZhuJpanel().addPrompt2("背包没有解锁!");
                    FormsManagement.showForm(33);
                    return;
                }

                if(StringUtils.isEmpty(buyCodeCard.getText())){
                    ZhuFrame.getZhuJpanel().addPrompt2("请输入要购买的点卡数");
                    return;
                }

                if(customerOrder == null){
                    ZhuFrame.getZhuJpanel().addPrompt2("#G请先选择一个!!");
                }else{
                    customerOrderExt = new TbSaleCardOrderExt();

                    BeanUtil.copyProperties(customerOrder, customerOrderExt);

                    customerOrderExt.setBuyCustomerCode(new BigDecimal(buyCodeCard.getText()));
                    OptionsJframe.getOptionsJframe().getOptionsJpanel().showBox(TiShiUtil.CONFIRM_BUY_CARD, customerOrderExt, "#Y你确定要花费 #G" + zongjia + "#Y 来购买 #R"+buyCodeCard.getText()+" #Y点仙玉?");
                }
            }
        });

        this.add(buy);


        this.add(leftArrows);
        leftArrows.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                if (dangqianyeshu > 1) {
                    dangqianyeshu--;
                    yema.setText(dangqianyeshu + "/" + zuidayema);

                    SearchGoodsBean searchGoodsBean1 = new SearchGoodsBean();
                    searchGoodsBean1.setSaletype(1);
                    searchGoodsBean1.setPageNum(dangqianyeshu);
                    String sendmes1 = Agreement.getAgreement().saleCardSearchAgreement(
                            GsonUtil.getGsonUtil().getgson().toJson(searchGoodsBean1));
                    SendMessageUntil.toServer(sendmes1);
                } else {
                    ZhuFrame.getZhuJpanel().addPrompt2("已经没有上一页了");
                }
            }
        });


        this.add(rightArrows);
        rightArrows.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseClicked(MouseEvent e) {
                // TODO Auto-generated method stub
                if (dangqianyeshu < zuidayema) {
                    dangqianyeshu++;
                    yema.setText(dangqianyeshu + "/" + zuidayema);
                    SearchGoodsBean searchGoodsBean1 = new SearchGoodsBean();
                    searchGoodsBean1.setSaletype(1);
                    searchGoodsBean1.setPageNum(dangqianyeshu);
                    String sendmes1 = Agreement.getAgreement().saleCardSearchAgreement(
                            GsonUtil.getGsonUtil().getgson().toJson(searchGoodsBean1));
                    SendMessageUntil.toServer(sendmes1);
                } else {
                    ZhuFrame.getZhuJpanel().addPrompt2("已经没有下一页了");
                }
            }
        });

        this.add(yema);
        add(this.buyCodeCard);
        this.add(jScrollPane, 10, -1);

        getBtnTeam();
        getBtnSkill();
    }

    private void setBuyCodeCard() {
        if (MyIsif.getStyle().equals("水墨UI")) {
            this.buyCodeCard.setBounds(205, 350, 88, 23);
        }else{
            this.buyCodeCard.setBounds(181, 365, 88, 23);
        }
        this.buyCodeCard.setOpaque(false);
        this.buyCodeCard.setBorder(BorderFactory.createEmptyBorder());
        this.buyCodeCard.setForeground(Color.white);
        this.buyCodeCard.setFont(new Font("楷体", 0, 15));
        this.buyCodeCard.setCaretColor(Color.white);
        buyCodeCard.addKeyListener(new KeyListener() {
            // 键盘监听
            @Override
            public void keyTyped(KeyEvent e) {
                // 让该输入框只能输入数字
                int keyChar = e.getKeyChar();
                if (!(keyChar >= KeyEvent.VK_0 && keyChar <= KeyEvent.VK_9)) {
                    e.consume();
                    return;
                }

                if (StringUtils.isNotEmpty(buyCodeCard.getText())) {
                    String key = buyCodeCard.getText();

                    if(key.indexOf("0") == 0 || StringUtils.isEmpty(dianka)){
                        e.consume();
                        return;
                    }
                    Long value = Long.parseLong(dianka); // 该框只能输入这个物品的最大数量
                    long l = Long.parseLong(key);

                    if(l >= value){
                        buyCodeCard.setText(value.toString());
                        e.consume();
                        return;
                    }

                    if (value < l || l <= 0){
                        e.consume();
                        return;
                    }


                }
            }

            @Override
            public void keyReleased(KeyEvent e) {
            }

            @Override
            public void keyPressed(KeyEvent e) {
                // TODO Auto-generated method stub
            }
        });
        this.buyCodeCard.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void removeUpdate(DocumentEvent e) {
                // TODO Auto-generated method stub
                String s = buyCodeCard.getText();
                String text = meidian;
                try {
                    if(s!=null && s.length() > 12){
                        return;
                    }
                    if(text!=null && text.length() > 12){
                        return;
                    }
                    if(buyCodeCard.getText() != null && !buyCodeCard.getText().equals("0") && !s.equals("0") && StringUtils.isNotEmpty(text)){
                        BigDecimal bigDecimal = new BigDecimal(s);
                        BigDecimal bigDecimal1 = new BigDecimal(text);
                        BigDecimal multiply = bigDecimal.multiply(bigDecimal1);
                        zongjia = multiply.toString();
                    }



                } catch (NumberFormatException e1) {
                }
            }

            @Override
            public void insertUpdate(DocumentEvent e) {
                // TODO Auto-generated method stub
                String s = buyCodeCard.getText();
                String text = meidian;
                try {
                    if(s!=null && s.length() > 12){
                        return;
                    }
                    if(text!=null && text.length() > 12){
                        return;
                    }



                    if(buyCodeCard.getText() != null && !buyCodeCard.getText().equals("0") && !s.equals("0") && StringUtils.isNotEmpty(text)){
                        BigDecimal bigDecimal = new BigDecimal(s);
                        if(bigDecimal.longValue() > Long.parseLong(dianka)){

                            new Thread(() -> {

                                buyCodeCard.setText(dianka);

                            }).start();
                            bigDecimal = new BigDecimal(dianka);
                        }

                        BigDecimal bigDecimal1 = new BigDecimal(text);
                        BigDecimal multiply = bigDecimal.multiply(bigDecimal1);
                        zongjia = multiply.toString();

                    }

                    if(!s.equals("0")){
                        BigDecimal bigDecimal = new BigDecimal(s);
                        if(bigDecimal.longValue() > 0){
                        }

                    }else{
                    }
                } catch (NumberFormatException e1) {
                }
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                // TODO Auto-generated method stub
            }


        });
    }

    private ImageIcon smIcon = CutButtonImage.getImage("inkImg/number/b9/b9-sm-buy-bg.png", 481,500);
    private ImageIcon hmIcon = CutButtonImage.getImage("inkImg/number/b9/b9-hm-buy-bg.png", 452,527);
    LoginResult login;

    @Override
    protected void paintComponent(Graphics g) {
		super.paintComponent(g);
        if (MyIsif.getStyle().equals("水墨UI")) {

            g.drawImage(smIcon.getImage(), 0, 0, 481,500, this);
            if(zongjia != null){
                Util.drawPrice(g, new BigDecimal(zongjia), 340,365);
            }

            if(meidian != null){
                Util.drawPrice(g, new BigDecimal(meidian), 205,398);
            }

            //画金币位置
            login = RoleData.getRoleData().getLoginResult();
            Util.drawPrice(g, login.getGold(), 340, 398);
        }else {
            g.drawImage(hmIcon.getImage(), 0, 0, 452,527, this);

            if(zongjia != null){
                Util.drawPrice(g, new BigDecimal(zongjia), 315,380);
            }

            if(meidian != null){
                Util.drawPrice(g, new BigDecimal(meidian), 181,410);
            }

            //画金币位置
            login = RoleData.getRoleData().getLoginResult();
            Util.drawPrice(g, login.getGold(), 314, 410);
        }

    }

    public CodeCardBtn getBtnTeam() {
        if(this.bjczBtn1 != null){
            this.remove(bjczBtn1);
            bjczBtn1 = null;
        }
        if (MyIsif.getStyle().equals("水墨UI")) {

                bjczBtn1 = new CodeCardBtn("inkImg/number/b9/b9-sm-buy-big.png", 1, 1, CodeCardJFrame.bjczMainJpanel);
                bjczBtn1.setBounds(65, 17, 85, 35);
                this.add(bjczBtn1);

        }else {

                bjczBtn1 = new CodeCardBtn("inkImg/number/b9/b9-hm-buy-big.png", 1, 1, CodeCardJFrame.bjczMainJpanel);
                bjczBtn1.setBounds(38, 30, 85, 35);
                this.add(bjczBtn1);

        }
        return bjczBtn1;
    }


    public CodeCardBtn getBtnSkill() {
        if(this.bjczBtn2 != null){
            this.remove(bjczBtn2);
            bjczBtn2 = null;
        }
        if (MyIsif.getStyle().equals("水墨UI")) {

                bjczBtn2 = new CodeCardBtn("inkImg/number/b9/b9-sm-sell-small.png", 1, 2, CodeCardJFrame.bjczMainJpanel);
                bjczBtn2.setBounds(160, 19, 85, 35);
                this.add(bjczBtn2);

        }else {

                bjczBtn2 = new CodeCardBtn("inkImg/number/b9/b9-hm-sell-small.png", 1, 2, CodeCardJFrame.bjczMainJpanel);
                bjczBtn2.setBounds(128, 29, 85, 35);
                this.add(bjczBtn2);

        }
        return bjczBtn2;
    }


    public static JTextField getFindName() {
        return findName;
    }

    public static void setFindName(JTextField findName) {
        CodeCardBuyJPanel.findName = findName;
    }

    public static JTextField getFindName1() {
        return findName1;
    }

    public static void setFindName1(JTextField findName1) {
        CodeCardBuyJPanel.findName1 = findName1;
    }

    public Goodstable getGood() {
        return good;
    }

    public void setGood(Goodstable good) {
        this.good = good;
    }

    public String[] getFvalue() {
        return Fvalue;
    }

    public void setFvalue(String[] fvalue) {
        Fvalue = fvalue;
    }

    public JTextArea getTestMes() {
        return testMes;
    }

    public void setTestMes(JTextArea testMes) {
        this.testMes = testMes;
    }

    public TbSaleCardOrder getCustomerOrder() {
        return customerOrder;
    }

    public void setCustomerOrder(TbSaleCardOrder customerOrder) {
        this.customerOrder = customerOrder;
    }

    public CodeCardSellerModel getCodeCardSellerModel() {
        return codeCardSellerModel;
    }

    public void setCodeCardSellerModel(CodeCardSellerModel codeCardSellerModel) {
        this.codeCardSellerModel = codeCardSellerModel;
    }

    public JScrollPane getjScrollPane() {
        return jScrollPane;
    }

    public void setjScrollPane(JScrollPane jScrollPane) {
        this.jScrollPane = jScrollPane;
    }

    public void setPage(int zuidayema) {
        this.zuidayema = zuidayema;
        yema.setText(this.dangqianyeshu + "/" + this.zuidayema);
    }

    public JLabel getDiankaLab() {
        return diankaLab;
    }

    public void setDiankaLab(JLabel diankaLab) {
        this.diankaLab = diankaLab;
    }

    public JLabel getZongjiaLab() {
        return zongjiaLab;
    }

    public void setZongjiaLab(JLabel zongjiaLab) {
        this.zongjiaLab = zongjiaLab;
    }

    public JLabel getDanjiaLab() {
        return danjiaLab;
    }

    public void setDanjiaLab(JLabel danjiaLab) {
        this.danjiaLab = danjiaLab;
    }

    public TbSaleCardOrderExt getCustomerOrderExt() {
        return customerOrderExt;
    }

    public void setCustomerOrderExt(TbSaleCardOrderExt customerOrderExt) {
        this.customerOrderExt = customerOrderExt;
    }
}

